Skip to content

Fix: count all tool tokens in budget and add summarization UserMessage priority#4992

Merged
bhavyaus merged 1 commit intomainfrom
dev/bhavyau/fix-tool-token-budget
Apr 6, 2026
Merged

Fix: count all tool tokens in budget and add summarization UserMessage priority#4992
bhavyaus merged 1 commit intomainfrom
dev/bhavyau/fix-tool-token-budget

Conversation

@bhavyaus
Copy link
Copy Markdown
Contributor

@bhavyaus bhavyaus commented Apr 6, 2026

Related: https://github.com/microsoft/vscode-internalbacklog/issues/7331

Problem

Two issues causing summarization failures:

1. Tool token under-counting (context_length_exceeded → "No messages provided")

The 3/30 change (#4834) excluded deferred tools from toolTokens, making the message budget ~31K too generous. While deferred tools use defer_loading: true in the main agent loop (so Anthropic does not count them), the summarization call uses ChatLocation.Other where all tools count fully. The overly generous budget causes conversations to grow larger than the actual context window can hold, leading to cascading summarization failures.

Fix: Count all tool tokens (including deferred) in the budget calculation. This is conservative for the main render but safe — and ensures summarization is triggered earlier before the context window overflows.

Also applies the same tool-token-aware budget reduction inside getSummary(Full) so the summarization prompt render leaves room for tool schemas.

2. Summarization UserMessage has no priority (empty messages)

The final "Summarize the conversation..." UserMessage in ConversationHistorySummarizationPrompt had no explicit priority (defaulting to 0). Under tight budgets, prompt-tsx would keep the high-priority SystemMessage (900) and prune the UserMessage, resulting in only system messages → Anthropic extracts those to system field → messages array is empty → "No messages provided".

Fix: Set priority={this.props.priority} on the UserMessage to match the SystemMessage priority.

Changes

  • agentIntent.ts: Count all tools for toolTokens (remove effectiveTools filtering)
  • summarizedConversationHistory.tsx: Add tool-token budget reduction in getSummary(Full), add priority to summarization UserMessage
  • summarization.spec.tsx: Add repro test for empty messages with small budget

Copilot AI review requested due to automatic review settings April 6, 2026 04:41
…ools

Deferred tools (defer_loading: true) still count against the API context
window. The 3/30 change (#4834) excluded them from toolTokens, causing
the message budget to be ~31K tokens too generous and leading to
context_length_exceeded errors followed by summarization failures
("No messages provided").
@bhavyaus bhavyaus force-pushed the dev/bhavyau/fix-tool-token-budget branch from f965703 to 2d70e5a Compare April 6, 2026 04:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses agent summarization failures by tightening token-budget calculations to account for tool schemas in summarization contexts and by preventing the summarization instruction UserMessage from being pruned under budget pressure.

Changes:

  • Count all tool tokens in the agent prompt budgeting path to avoid overestimating available message budget when summarization runs with fully-counted tools.
  • Reserve tool-token budget when rendering the Full-mode summarization prompt, and give the summarization instruction UserMessage an explicit priority.
  • Add a unit test capturing a “No messages provided” repro scenario under extremely small token budgets.
Show a summary per file
File Description
src/extension/prompts/node/agent/test/summarization.spec.tsx Adds a repro-oriented test around empty rendered message arrays under tiny budgets.
src/extension/prompts/node/agent/summarizedConversationHistory.tsx Ensures summarization instruction UserMessage has priority; reserves prompt budget for tool schemas in Full summarization mode.
src/extension/intents/node/agentIntent.ts Changes tool token counting to include all tools (including deferred) and removes tool filtering for summarization prompt context.

Copilot's findings

  • Files reviewed: 6/6 changed files
  • Comments generated: 3

@bhavyaus bhavyaus enabled auto-merge April 6, 2026 04:50
@bhavyaus bhavyaus added this pull request to the merge queue Apr 6, 2026
Merged via the queue into main with commit 3f2a72e Apr 6, 2026
19 checks passed
@bhavyaus bhavyaus deleted the dev/bhavyau/fix-tool-token-budget branch April 6, 2026 06:47
@bhavyaus bhavyaus added the ~release-cherry-pick Trigger: cherry-pick this PR to the latest release branch label Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

~release-cherry-pick Trigger: cherry-pick this PR to the latest release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants